<?php
require_once __DIR__ . '/../../vendor/autoload.php';

$connection = new \PhpAmqpLib\Connection\AMQPStreamConnection('127.0.0.1', 5672, 'guest', 'guest');

$channel = $connection->channel();
// 定义队列
$channel->queue_declare('first', false, true,false, false);
$channel->basic_qos(0, 1, false);
var_dump('111111111');
$callback = function ($msg){

    // 接收到消息
    $content = json_decode($msg->body,true);

    // 对数据库操作
//    $con = mysqli_connect('106.15.183.188','new_nydata','new_nydata','new_nydata');
    $con = mysqli_connect('localhost','root','root','new_nydata');
    if(!$con){
        var_dump('connect error');
    }

    // 上传设备状态
    if($content['type'] == 2){
        // 传感器状态
        if($content['is_sen'] == 1){
            $sql = "update  nyd_sensor set is_bad =".$content->status."where sensor_mac = '".$content->id."'";
        }
        // 设备状态
        else{
            $sql = "update  nyd_data set is_bad =".$content->status."where dev_mac = '".$content->id."'";
        }

    } // 上传汇总数据
    elseif ($content['type'] == 3) {

        // 把设备和传感器的mac_id和对应的自增id放入redis中
        $Redis = new Redis();
        $Redis->connect('127.0.0.1', 6379);

        // 设备
        $dev_sql = "select id,dev_mac from nyd_dev";
        $dev_res = mysqli_query($con, $dev_sql);
        while ($row = $dev_res->fetch_assoc()) {
            if (!$Redis->get($row['dev_mac'])) {
                $Redis->set($row['dev_mac'], $row['id']);
            }
        }

        // 传感器
        $sec_sql = "select id,sensor_mac  from nyd_sensor";
        $sec_res = mysqli_query($con, $sec_sql);
        while ($row = $sec_res->fetch_assoc()) {
            if (!$Redis->get($row['sensor_mac'])) {
                $Redis->set($row['sensor_mac'], $row['id']);
            }
        }

        // 剥离数据 分别入库
        $insert_data = "";
        $data = $content['data'];

        for ($i = 0; $i < count($data); $i++) {
            $mac_id = $Redis->get($data[$i]['id']);
            if ($i != count($data) - 1) {
                $insert_data .= "(" . $mac_id . ",'" . $data[$i]['data'] . "'," . $data[$i]['is_open'] . "," . $data[$i]['type'] . ",'" . date("Y-m-d H:i:s", time()) . "','" . $content['gateway_id'] . "'," . $content['house_id'] . "),";
            } else {
                $insert_data .= "(" . $mac_id . ",'" . $data[$i]['data'] . "','" . $data[$i]['is_open'] . "'," . $data[$i]['type'] . ",'" . date("Y-m-d H:i:s", time()) . "','" . $content['gateway_id'] . "'," . $content['house_id'] . ")";
            }
        }

        $sql = "insert into nyd_data (dev_id,data,is_open,type,now_time,gateway_id,house_id) VALUES " . $insert_data;

    }
//    var_dump($sql);
    if(mysqli_query($con,$sql)){
        var_dump('Success');
    }else{
        var_dump('Error         '.$sql);
    }
        $con->close();

        // 接收到消息后确认
        $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);

};

$channel->basic_consume('first', '', false, false, false, false,$callback );
while(count($channel->callbacks)) {
    $channel->wait();
}

$channel->close();
$connection->close();